package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.ProdTecRoutProcTechDrawing;
import com.mxpio.erp.common.technology.entity.ProdTecRoutProcTechDrawingSignIn;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcTechDrawingService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class ProdTecRoutProcTechDrawingServiceImpl extends BaseServiceImpl<ProdTecRoutProcTechDrawing> implements ProdTecRoutProcTechDrawingService {

    @Transactional
    @Override
    public ProdTecRoutProcTechDrawing save(ProdTecRoutProcTechDrawing entity){
        if(StringUtils.isEmpty(entity.getFamily())){
            entity.setFamily(UUID.randomUUID().toString().replace("-",""));
        }
        super.save(entity);
        return entity;
    }

    @Override
    @Transactional
    public Result<ProdTecRoutProcTechDrawingSignIn> signInApply(ProdTecRoutProcTechDrawingSignIn prodTecRoutProcTechDrawingSignIn) {
        String drawingFileId = prodTecRoutProcTechDrawingSignIn.getDrawingFileId();
        ProdTecRoutProcTechDrawing file = this.getById(ProdTecRoutProcTechDrawing.class, drawingFileId);
        if (file==null){
            return Result.error("入参中的drawingFileId无效，未能匹配当前版本数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许签入操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人本人可以签入操作");
        }
        Long count = JpaUtil.linq(ProdTecRoutProcTechDrawingSignIn.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的签入申请，请勿重复提交申请");
        }
        ProdTecRoutProcTechDrawingSignIn signIn = new ProdTecRoutProcTechDrawingSignIn();
        signIn.setDrawingFileId(drawingFileId);
        signIn.setSignInDesc(prodTecRoutProcTechDrawingSignIn.getSignInDesc());
        signIn.setFileNo(prodTecRoutProcTechDrawingSignIn.getFileNo());
        signIn.setFileName(prodTecRoutProcTechDrawingSignIn.getFileName());
        signIn.setLength(prodTecRoutProcTechDrawingSignIn.getLength());
        signIn.setRemarks(prodTecRoutProcTechDrawingSignIn.getRemarks());
        signIn.setExtension(prodTecRoutProcTechDrawingSignIn.getExtension());
        signIn.setFamily(file.getFamily());
        signIn.setItemCode(prodTecRoutProcTechDrawingSignIn.getItemCode());
        signIn.setRoutProcId(prodTecRoutProcTechDrawingSignIn.getRoutProcId());
        JpaUtil.save(signIn);

        return Result.OK("签入申请提交成功",signIn);
    }

    @Override
    @Transactional
    public List<ProdTecRoutProcTechDrawing> list(Criteria criteria) {
        List<ProdTecRoutProcTechDrawing> fileList = JpaUtil.linq(ProdTecRoutProcTechDrawing.class).where(criteria).list();
        if (!fileList.isEmpty()){
            for (ProdTecRoutProcTechDrawing file:fileList){//关联对应的变更、签入记录
                ProdTecRoutProcTechDrawingSignIn signIn = JpaUtil.linq(ProdTecRoutProcTechDrawingSignIn.class).equal("code", file.getSignInCode()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode()).findOne();
                if (signIn!=null){
                    file.setSignIn(signIn);
                }
            }
        }
        return fileList;
    }

    @Override
    @Transactional
    public Result<ProdTecRoutProcTechDrawing> signOutCancle(String id) {
        ProdTecRoutProcTechDrawing file = this.getById(ProdTecRoutProcTechDrawing.class, id);
        if (file==null){
            return Result.error("未匹配到数据");
        }
        if (!file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
            return Result.error("仅签出操作状态下允许取消签出操作");
        }
        if (!file.getSignOffer().equals(SecurityUtils.getLoginUsername())){
            return Result.error("仅签出人自己可以取消签出操作");
        }

        Long count = JpaUtil.linq(ProdTecRoutProcTechDrawingSignIn.class).equal("family", file.getFamily()).equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()).count();
        if (count>0){
            return Result.error("已经有审核中的签入申请，无法取消签出操作");
        }
        file.setSignOffFlag(CommonEnums.YesNo.NO.getCode());
        file.setSignOffer(null);
        file.setSignOffTime(null);
        this.update(file);
        return Result.OK(file);
    }
}
